package com.uhylab.uhuvinetest.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.uhylab.uhuvinetest.JNIJson;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.dao.CloudHistory;
import com.uhylab.uhuvinetest.dao.CloudHistoryDao;
import com.uhylab.uhuvinetest.dao.DaoSingleton;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称：Uhylab
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/6/3 14:52
 * 修改人：xyb
 * 修改时间：16/6/3 14:52
 * 修改备注：
 */
public class UHCharWebActivity extends UHBaseActivity {


    @BindView(R.id.charWeb_text)
    TextView mCharWebText;
    @BindView(R.id.charWeb_back)
    ImageView mCharWebBack;
    @BindView(R.id.char_Web)
    WebView mCharWeb;
    @BindView(R.id.textView)
    TextView textView;

    private CloudHistoryDao cloudHistoryDao;
    private String mTitle;
    private ArrayList<String> mDateList;
    private ArrayList<Integer> mIntegers;
    private ArrayList<String> mType;
    private int mIntExtra;
    private String mJSDate;
    private String mDas;


    private JNIJson jniJson;


    @Override
    public int bindLayout() {
        return R.layout.activity_charweb;
    }

    @Override
    public void initView(View view) {
        ButterKnife.bind(this);
        jniJson = new JNIJson(this);
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void doBusiness(Context mContext) {
        // 历史数据
        cloudHistoryDao = DaoSingleton.getInstance().getCloudHistoryDao();
        Intent intent = getIntent();
        mTitle = intent.getStringExtra("title");
        String describe;
        describe = intent.getStringExtra("describe");
        mIntExtra = intent.getIntExtra("position", 0);
        textView.setText(describe);
        initDataList();
        String url = "file:///android_asset/" + "Echars/" + "Echarts.html";
        mCharWebText.setText(mTitle);
        WebSettings settings = mCharWeb.getSettings();
        settings.setJavaScriptEnabled(true);
        mCharWeb.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Toast.makeText(UHCharWebActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                String loadUrl = "javascript:initEchartBar(" + mJSDate + "," + mDas + "," + "'" + mTitle + "'" + ");";
                loadUrl = loadUrl.replaceAll("\"", "\'");
                mCharWeb.loadUrl(loadUrl);

            }
        });
        mCharWeb.loadUrl(url);
    }

    private void initDataList() {
        List<CloudHistory> cloudHistories = cloudHistoryDao.loadAll();
        mDateList = new ArrayList<>();
        mIntegers = new ArrayList<>();
        int what = 0;
        for (CloudHistory cloudHistory :
                cloudHistories) {

            String date = cloudHistory.getCreatetime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                if (!(date == null || date.equals(""))) {
                    Date parse = sdf.parse(date);
                    DateFormat data = new SimpleDateFormat("MM/dd");
                    String formatData = data.format(parse);
                    boolean isAdd = false;
                    if (mDateList.size() == 0) {
                        mDateList.add(formatData);
                        mIntegers.add(what);
                    } else {
                        if (!mDateList.contains(formatData)) {
                            if (mDateList.size() < 7) {
                                isAdd = true;
                            }
                        }
                    }
                    if (isAdd) {
                        mDateList.add(formatData);
                        mIntegers.add(what);
                    }
                    what++;
                }
            } catch (ParseException e) {
                e.printStackTrace();
                Log.e("UHCharWebActivity", "出错了");
            }
        }
        // TODO: 16/6/3 谢彦斌 更新时间的进度 转换成 json格式的
        Gson gson = new Gson();
        mJSDate = gson.toJson(mDateList);

        mType = new ArrayList<>();


        for (int i = 0; i < mIntegers.size(); i++) {
            switch (mIntExtra) {
                case 0:
                    float v = (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) < 260) ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) + 180 : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) + 360);
                    if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) < 260)) {
                        v = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) + 180;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) < 180)){
                        v = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) + 360;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) >= 260)){
                        v = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getLeukocytes()) ;
                    }
                    mType.add(getNear(jniJson.getS(), v) + "");
                    break;
                case 1:
                    float v1 = (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) < 260) ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) + 180 : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) + 360);
                    if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) < 260)) {
                        v1 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) + 180;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) < 180)){
                        v1 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) + 360;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) >= 260)){
                        v1 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getNitrite()) ;
                    }
                    mType.add(getNear(jniJson.getNis(), v1) + "");
                    break;
                case 2:
                    float v2 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getUrobilinogen()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getUrobilinogen()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getUrobilinogen()) + 360);
                    mType.add(getNear(jniJson.getUrs(), v2) + "");
                    break;
                case 3:
//                    float v3 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getProtein()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getProtein()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getProtein()) + 360);
                    float v3 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getProtein());
                    mType.add(getNear(jniJson.getPrs(), v3) + "");
                    break;
                case 4:
                    float v4 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getPh()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getPh()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getPh()) + 360);
                    mType.add(getNear(jniJson.getPhs(), v4) + "");
                    break;
                case 5:
                    float v5 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBlood()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBlood()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBlood()) + 360);
                    mType.add(getNear(jniJson.getBls(), v5) + "");
                    break;
                case 6:
                    float v6 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getGravity()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getGravity()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getGravity()) + 360);
                    mType.add(getNear(jniJson.getSpecifis(), v6) + "");
                    break;
                case 7:
                    float v7 = (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) < 260) ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) + 180 : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) + 360);
                    if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) < 260)) {
                        v7 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) + 180;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) < 180)){
                        v7 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) + 360;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) >= 260)){
                        v7 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getKetones()) ;
                    }
                    mType.add(getNear(jniJson.getKes(), v7) + "");
                    break;
                case 8:
//                    float v8 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) + 360);
                    float v8 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin());
//                    if ((v8 < 20)) {
//                        v8 = v8 + 20;
                    if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) > 180) && (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) < 260)) {
                        v8 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) + 180;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) < 180)){
                        v8 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) + 360;
                    }else if ((Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) >= 260)){
                        v8 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getBilirubin()) ;
                    }


//                    }
                    mType.add(getNear(jniJson.getBis(), v8) + "");
                    break;
                case 9:
                    float v9 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getGlucose());
                    mType.add(getNear(jniJson.getGls(), v9) + "");
                    break;
                case 10:
                    float v10 = Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getAscorbicacid()) > 180 ? Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getAscorbicacid()) : (Float.parseFloat(cloudHistories.get(mIntegers.get(i)).getAscorbicacid()) + 360);
                    mType.add(getNear(jniJson.getAss(), v10) + "");
                    break;

            }
        }
        mDas = gson.toJson(mType);

    }


    public static int getNear(float[] array, float target) {
        if (array.length == 0) return -1;
        if (array.length == 1) return 0;
        float[] floa = new float[array.length];
        for (int k = 0; k < floa.length; k++) {
            floa[k] = array[k];
        }
        Arrays.sort(array);
        int min = 0;
        int max = array.length - 1;
        int mid = (int) (min + max) / 2;

        while (max - min > 1) {
            if (target == array[mid]) return mid;
            if (target < array[mid]) max = mid;
            if (target > array[mid]) min = mid;
            mid = (int) (min + max) / 2;
        }

        for (int j = 0; j < floa.length; j++) {
            if (floa[j] == array[array[max] - target < target - array[min] ? max : min]) {
                System.out.println(j + "....." + floa[j]);
                System.out.println("i:" + (array[max] - target < target - array[min] ? max : min));
                return j;
            }
        }
        return array[max] - target < target - array[min] ? max : min;
    }


    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }


    @OnClick(R.id.charWeb_back)
    public void onClick() {
        finish();
    }
}
